//姓名框失去焦点 ajax请求对应的用户id
$(document).ready(function (){
    var cid = getQueryVariable("cid");
    $('.searchName').blur(function () {
        var val = $(this).val();
        var $that = $(this);
        $('.searchDids').val('');
        if (val!=null&&val!='') {
            $.ajax({
                "url": "customerController/dietitian/search/" + val+"/"+cid,
                "type": "get",
                "dataType":"json",
                "success": function (dto) {
                    //如果找出的id集合数量大于0 就保存为条件
                    if (dto.tList.length > 0){
                        $('.searchDids').val(dto.tList);
                    } else{
                        alert("没有相关的用户");
                        $that.val('');
                    }
                }
            });
        }
 
    });

});



$(document).ready(function () {
    //日期二选项是否显示
    $("select[name='op']").change(function () {
         var op = $("select[name='op'] > option:selected").val();
         if (op ==3){
             $("input[name='mbirthday2']").css("display","inline-block");
         }else{
             $("input[name='mbirthday2']").css("display","none");
         }
    });

    $("#search").click(function () {
        var fdName1 = "dids";
        var value1 = $("input[name='dids']").val();

        var fdName2 = "msex";
        var value2 = $("input[name='msex']:checked").val();

        var fdName3 = "edate";
        var value3 = $("input[name='edate']").val();
        var value4 = $("input[name='edate2']").val();
        var op3 = $("select[name='op'] > option:selected").val();

        //默认没有填条件不允许搜索，但是找到一个条件不为空 则允许搜索
        var flag = false;
        var conditions = new Array();//空的条件数组

        var conditionsInit = [new Condition(fdName1,value1),new Condition(fdName2,value2),new Condition(fdName3,value3,op3,value4)];//初始化数组讲全部条件都装入数据（即便为空）
        for(var i=0;i<conditionsInit.length;i++){
            var condition = conditionsInit[i];
            console.log(condition.getFdName() + ' '+condition.getValue() + ' '+condition.getValue2());
            console.log(condition.getValue()!=''+ " " +condition.getValue()!=null);
           if (condition.getValue()!=''&&condition.getValue()!=null&condition.getValue()!='%') {
               flag = true;//如果条件成立说明允许搜索，且这个条件加入条件数组
               conditions.push(condition);
           }

        }



        // 条件成立才发送ajax请求

        if(flag) {
            if (op3==3&&(!isValidateDate(value3)||!isValidateDate(value4))) {
                alert("between and 需要 2个值");
                return;
            }

            //console.log(JSON.stringify(conditions));
            var cid = getQueryVariable("cid");
            conditions.push(new Condition("cid",cid,1));
            alert(JSON.stringify(conditions));
            searchAjax(conditions,1);
        }else{
            alert("请填写条件");
            return ;
        }

        //封装的搜索ajax
        function searchAjax(conditions,currPage) {
            alert(JSON.stringify(conditions));
            $.ajax({
                "url": "employrecordController/dietitians/search/"+currPage,
                "type": "post",
                "contentType": "application/json",
                "data":JSON.stringify(conditions)
                //         "data": JSON.stringify(
                //         [{
                //                 "fdName":"user667",
                //                 "value":"a123456",
                //                 "op":"19"},{
                //             "fdName":"user666",
                //             "value":"a123456",
                //             "op":"19"}]
                // )

                ,
                //JSON.stringify(conditions),//将js数组抓换成json对象格式发送给服务器 springmvc @RequestBody 注解才能处理
                "dataType": "json",
                "success": function (dto) {
                    console.log(dto);
                    var pageInfo = dto.obj;
                    var members = dto.tList;
                    if (dto.code==200) {
                        $("#employrecords ul li:gt(0)").remove();
                        showListAttribute(dto.tList,$("#employrecords ul li").eq(0));


                    }else{
                        alert(dto.msg);
                    }
                }

            });
        }

        function initSearch(pageInfo,conditions){
            $(".pageList").html("");
            var pageCount = (pageInfo.itemCount + pageInfo.pageSize - 1) / pageInfo.pageSize

            var pageUl = "<ul class='pageUl'>" +
                "<li><a  alt='1'>首页</a></li>" +
                "<li><a   alt='"+ (pageInfo.currPage - 1)+"'>上一页</a></li>" +
                "<li><a   alt='"+ (pageInfo.currPage + 1)+"'>下一页</a></li>" +
                "<li><a  alt='"+ pageCount +"'>尾页</a></li>" +
                "</ul>";
            //添加js版的分页
            $(".pageList").append(pageUl);
            $(".pageUl > li > a").click(function () {

                var currPageOp = $(this).attr('alt');//希望操作的页码
                //alert(currPage);
                var currPage = pageInfo.currPage;
                if ((currPage == 1 && (currPageOp == 1 || currPageOp == 0))||(currPage == pageCount && (currPageOp == pageCount || currPageOp == pageCount + 1))){

                } else
                {
                    searchAjax(conditions, currPageOp);
                }
            });
        }

    });

});